<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <style>

        .ht{
            text-align: center;
            color: cadetblue;
            font-family: 幼圆;
        }
        .tab{
            width: 500px;
            border: 5px solid cadetblue;
            margin: 0px auto;
            border-radius: 5px;
            font-family: 幼圆;
        }
        .ltr td{
            border: 1px solid  powderblue;

        }
        .ipt{
            border: 0px;
            width: 50%;

        }
        .btn1{
            border: 2px solid powderblue;
            border-radius: 4px;
            width:60px;
            background-color: antiquewhite;

        }

        .msg {
            color: gold;
        }

        .buttonContainer{
            text-align: center;
        }
    </style>

    <script>
        function checkUsername(){
            var usernameReg = /^\d{10}$/
            var usernameInput = document.getElementById("usernameInput")
            var username = usernameInput.value
            var usernameMsg = document.getElementById("usernameMsg")
            if(!usernameReg.test(username)){
                usernameMsg.innerText="格式有误"
                return false
            }
            usernameMsg.innerText="OK"
            return true
        }


        function checkUserPwd(){
            var userPwdReg =  /^[a-zA-Z0-9]{8,15}$/
            var userPwdInput = document.getElementById("passWord")
            var userPwd = userPwdInput.value
            var userPwdMsg = document.getElementById("userPwdMsg")
            if(!userPwdReg.test(userPwd)){
                userPwdMsg.innerText="格式有误"
                return false
            }
            userPwdMsg.innerText="OK"
            return true
        }


        function checkReUserPwd(){
            var userPwdReg =  /^[a-zA-Z0-9]{8,15}$/
            // 再次输入的密码的格式
            var reUserPwdInput = document.getElementById("repassWord")
            var reUserPwd = reUserPwdInput.value
            var reUserPwdMsg = document.getElementById("reUserPwdMsg")
            if(!userPwdReg.test(reUserPwd)){
                reUserPwdMsg.innerText="格式有误"
                return false
            }
            // 获得上次密码,对比两次密码是否一致
            var userPwdInput = document.getElementById("passWord")
            var userPwd = userPwdInput.value
            if(reUserPwd != userPwd){
                reUserPwdMsg.innerText="两次密码不一致"
                return false
            }
            reUserPwdMsg.innerText="OK"
            return true
        }


        function checkForm(){
            var flag1 = checkUsername()
            var flag2 = checkUserPwd()
            var flag3 = checkReUserPwd()

            return flag1 && flag2 && flag3
        }


    </script>



</head>
<body>
<h1 class="ht">欢迎使用日程管理系统</h1>
<h3 class="ht">请注册</h3>
<form id="registForm" method="post" onsubmit="return checkForm()">
    <table class="tab" cellspacing="0px">
        <tr class="ltr">
            <td>请输入学号</td>
            <td>
                <input class="ipt" id="usernameInput" type="text" name="userName" onblur="checkUsername()">
                <span id="usernameMsg" class="msg"></span>
            </td>
        </tr>
        <tr class="ltr">
            <td>请输入密码</td>
            <td>
                <input class="ipt" id="passWord" type="password" name="passWord" onblur="checkUserPwd()">
                <span id="userPwdMsg" class="msg"></span>
            </td>
        </tr>
        <tr class="ltr">
            <td>确认密码</td>
            <td>
                <input class="ipt" id="repassWord" type="password" name="repassWord" onblur="checkReUserPwd()">
                <span id="reUserPwdMsg" class="msg"></span>
            </td>
        </tr>

        <tr class="ltr"></tr>
        <td>
            <label for="user-role">请选择您的身份：</label>
            <select name="ofManager" id="user-role">
                <option value=true>管理员</option>
                <option value=false selected>学生</option>
            </select>
        </td>
        </tr>

        <tr class="ltr">
            <td colspan="2" class="buttonContainer">
                <input class="btn1" type="submit" value="注册" onclick="submitForm()">
                <input class="btn1" type="reset" value="重置">
                <button class="btn1"><a  href="Login.html">去登录</a></button>
            </td>
        </tr>
    </table>

</form>

<script>
    function submitForm() {
        event.preventDefault();
        const form = document.getElementById('registForm');
        const formData = new FormData(form);
        const jsonData = {};
        for (let [key, value] of formData.entries()) {
            jsonData[key] = value;
        }
        const options = {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(jsonData)
        };
        fetch('../user/regist', options)
            .then(response => response.json())
            .then(data => {

                if (data.code==302){
                    alert("注册成功,请完善信息")
                    console.log(data);
                    let ReAddress=data.data
                    window.location.assign(ReAddress);
                }

                if (data.code==500){
                    alert(data)
                }

            })
            .catch(error => {
                console.error('Error:', error);
                alert("网络异常");
            });
    }

</script>
</body>
</html>